# Replication Archive for: 
# Coppock, Alexander and Donald P. Green. 2020. 
# "Do Belief Systems Exhibit Dynamic Constraint?" 
# The Journal of Politics, Forthcoming.

rm(list = ls())

library(tidyverse)
library(psych)

dat <- read_csv("data/raw/studies_1_2_lucid_raw.csv")
dat <- dat[-c(1:4), ]

# Load factor analysis models
load("data/raw/factor_analysis_models.rdata")

# Load Mturk dataset
study_1_mturk <- read_rds("data/clean/study_1_mturk_cleaned.rds")

# Lucid demographic recodes -----------------------------------------------

dat <-
  dat %>%
  mutate(
    lucid_age = as.numeric(age_1),
    lucid_education =
      case_when(
        education_1	== 1 ~ "Some high school or less",
        education_1	== 2 ~ "High school graduate",
        education_1	== 3 ~ "Other post high school vocational training",
        education_1	== 4 ~ "Completed some college, but no degree",
        education_1	== 5 ~ "Associate's degree",
        education_1	== 6 ~ "Bachelor's degree",
        education_1	== 7 ~ "Master's or professional degree",
        education_1	== 8 ~ "Doctorate degree",
        education_1	== -3105 ~ "None of the above"
      ),
    
    lucid_ethnicity =
      case_when(
        ethnicity	== 1 ~ "White",
        ethnicity	== 2 ~ "Black, or African American",
        ethnicity	== 3 ~ "American Indian or Alaska Native",
        ethnicity	== 4 ~ "Asian *** Asian Indian",
        ethnicity	== 5 ~ "Asian *** Chinese",
        ethnicity	== 6 ~ "Asian *** Filipino",
        ethnicity	== 7 ~ "Asian *** Japanese",
        ethnicity	== 8 ~ "Asian *** Korean",
        ethnicity	== 9 ~ "Asian *** Vietnamese",
        ethnicity	== 10 ~ "Asian *** Other",
        ethnicity	== 11 ~ "Pacific Islander *** Native Hawaiian",
        ethnicity	== 12 ~ "Pacific Islander *** Guamanian",
        ethnicity	== 13 ~ "Pacific Islander *** Samoan",
        ethnicity	== 14 ~ "Pacific Islander *** Other Pacific Islander",
        ethnicity	== 15 ~ "Some other race",
        ethnicity	== 16 ~ "Prefer not to answer"
      ),
    
    lucid_gender =
      case_when(gender == 1 ~ "Male",
                gender == 2 ~ "Female"),
    
    lucid_female = as.numeric(lucid_gender == "Female"),
    
    lucid_hhi =
      case_when(
        hhi == 1 ~ "Less than $14,999",
        hhi == 2 ~ "$15,000 to $19,999",
        hhi == 3 ~ "$20,000 to $24,999",
        hhi == 4 ~ "$25,000 to $29,999",
        hhi == 5 ~ "$30,000 to $34,999",
        hhi == 6 ~ "$35,000 to $39,999",
        hhi == 7 ~ "$40,000 to $44,999",
        hhi == 8 ~ "$45,000 to $49,999",
        hhi == 9 ~ "$50,000 to $54,999",
        hhi == 10 ~ "$55,000 to $59,999",
        hhi == 11 ~ "$60,000 to $64,999",
        hhi == 12 ~ "$65,000 to $69,999",
        hhi == 13 ~ "$70,000 to $74,999",
        hhi == 14 ~ "$75,000 to $79,999",
        hhi == 15 ~ "$80,000 to $84,999",
        hhi == 16 ~ "$85,000 to $89,999",
        hhi == 17 ~ "$90,000 to $94,999",
        hhi == 18 ~ "$95,000 to $99,999",
        hhi == 19 ~ "$100,000 to $124,999",
        hhi == 20 ~ "$125,000 to $149,999",
        hhi == 21 ~ "$150,000 to $174,999",
        hhi == 22 ~ "$175,000 to $199,999",
        hhi == 23 ~ "$200,000 to $249,999",
        hhi == 24 ~ "$250,000 and above",
        hhi == -3105 ~ "Prefer not to answer"
      ),
    lucid_hispanic_full =
      case_when(
        hispanic == 1 ~ "No , not of Hispanic, Latino, or Spanish origin",
        hispanic == 2 ~ "Yes, Mexican, Mexican American, Chicano",
        hispanic == 3 ~ "Yes, Cuban",
        hispanic == 4 ~ "Yes, another Hispanic, Latino, or Spanish origin *** Argentina ",
        hispanic == 5 ~ "Yes, another Hispanic, Latino, or Spanish origin *** Colombia ",
        hispanic == 6 ~ "Yes, another Hispanic, Latino, or Spanish origin *** Ecuador ",
        hispanic == 7 ~ "Yes, another Hispanic, Latino, or Spanish origin *** El Salvadore ",
        hispanic == 8 ~ "Yes, another Hispanic, Latino, or Spanish origin *** Guatemala ",
        hispanic == 9 ~ "Yes, another Hispanic, Latino, or Spanish origin *** Nicaragua ",
        hispanic == 10 ~ "Yes, another Hispanic, Latino, or Spanish origin *** Panama ",
        hispanic == 11 ~ "Yes, another Hispanic, Latino, or Spanish origin *** Peru ",
        hispanic == 12 ~ "Yes, another Hispanic, Latino, or Spanish origin *** Spain ",
        hispanic == 13 ~ "Yes, another Hispanic, Latino, or Spanish origin *** Venezuela ",
        hispanic == 14 ~ "Yes, another Hispanic, Latino, or Spanish origin *** Other Country",
        hispanic == 15 ~ "Prefer not to answer"
      ),
    lucid_hispanic = as.numeric(hispanic != 1),
    
    lucid_pid_7n =
      case_when(
        political_party	== 1 ~ 1,
        political_party	== 2 ~ 2,
        political_party	== 3 ~ 3,
        political_party	== 4 ~ 4,
        political_party	== 5 ~ 5,
        political_party	== 6 ~ 3,
        political_party	== 7 ~ 4,
        political_party	== 8 ~ 5,
        political_party	== 9 ~ 6,
        political_party	== 10  ~ 7
      ),
    lucid_pid_7 =
      case_when(
        lucid_pid_7n == 1 ~ "Strong Democrat",
        lucid_pid_7n == 2 ~ "Not a strong Democrat",
        lucid_pid_7n == 3 ~ "Lean Democrat",
        lucid_pid_7n == 4 ~ "Pure Independent",
        lucid_pid_7n == 5 ~ "Lean Republican",
        lucid_pid_7n == 6 ~ "Closer to Republican",
        lucid_pid_7n == 7 ~ "Strong Republican"
      ),
    lucid_zip = zip
  )

dat <-
  dat %>%
  mutate(
    pid_7 =
      case_when(
        pid_3 == "Democrat" & pid_dem == "Strong Democrat" ~ 1,
        pid_3 == "Democrat" & pid_dem == "Not very strong Democrat" ~ 2,
        pid_3 == "Democrat" & is.na(pid_dem) ~ 2,
        pid_3 %in% c("Independent", "Other:") & pid_ind == "Closer to Democratic Party" ~ 3,
        pid_3 %in% c("Independent", "Other:") & pid_ind == "Neither" ~ 4,
        pid_3 %in% c("Independent", "Other:") & is.na(pid_ind) ~ 4,
        pid_3 %in% c("Independent", "Other:") & pid_ind == "Closer to Republican Party" ~ 5,
        pid_3 == "Republican" & is.na(pid_rep) ~ 6,
        pid_3 == "Republican" & pid_rep == "Not very strong Republican" ~ 6,
        pid_3 == "Republican" & pid_rep == "Strong Republican" ~ 7,
        TRUE ~ 4
      ),
    pid_3_cat =
      case_when(
        pid_7 %in% 1:3 ~ "Democrat",
        pid_7 %in% 4 ~ "Independent",
        pid_7 %in% 5:7 ~ "Republican"
      ),
    ideo_3_cat =
      case_when(
        ideology == "Liberal" ~ "Liberal",
        ideology %in% c("Moderate", "Libertarian", "Other", "Something else", "Haven't thought much about this") ~ "Other",
        ideology %in% "Conservative" ~ "Conservative"
      ),
    
    ideo_5 =
      case_when(
        ideology == "Liberal" ~ "Liberal",
        ideology == "Libertarian" ~ "Libertarian",
        ideology == "Moderate" ~ "Moderate",
        ideology %in% "Conservative" ~ "Conservative",
        ideology %in% c("Other", "Something else", "Haven't thought much about this") ~ "Other",
        TRUE ~ "Moderate"
      ),
    
    education = if_else(is.na(education), "Some College", education),
    
    educ_5_cat =
      factor(
        education,
        levels = c(
          "Less than High School",
          "High School / GED",
          "Some College",
          "4-year College Degree",
          "Graduate School"
        )
      ),
    
    educ_5 = fct_recode(educ_5_cat, 
                        College = "4-year College Degree",
                        `High School` = "High School / GED"), 
    
    race_4 =
      case_when(
        race %in% c("White, non Hispanic") ~ "White",
        race %in% c("African-American") ~ "Black",
        race %in% c("Hispanic") ~ "Hispanic",
        race %in% c("Native American", "Asian/Pacific Islander") ~ "Other",
        TRUE ~ "White"
      ),
    
    female = case_when(gender == "Female" ~ 1,
                       TRUE ~ 0),
    
    age_5 =
      case_when(
        age %in% c("18 to 29") ~ 1,
        age %in% c("30 to 39") ~ 2,
        age %in% c("40 to 49") ~ 3,
        age %in% c("50 to 59") ~ 4,
        age %in% c("60 to 69", "70 or over") ~ 5,
        TRUE ~ 5
      ),
    
    age_5 = factor(age_5, levels = 1:5, labels = c("18 - 29", "30 - 39", "40 - 49", "50 - 59", "60+")),
    
    Z = relevel(factor(Z), ref = "control")
  )

dat <-
  dat %>%
  mutate(
    #Recode Outcome Measures so larger # indicate more agreement with author
    
    dv_climate_1_w1 = as.numeric(climate_1_1),
    dv_climate_2_w1 = as.numeric(climate_2_1),
    dv_climate_3_w1 = -1 * as.numeric(climate_3_1) + 8,
    dv_climate_4_w1 = -1 * as.numeric(climate_4_1) + 8,
    dv_climate_5_w1 = -1 * as.numeric(climate_5_1),
    
    dv_wall_1_w1 = as.numeric(wall_1_1),
    dv_wall_2_w1 = -1 * as.numeric(wall_2_1) + 101,
    dv_wall_3_w1 = -1 * as.numeric(wall_3_1) + 8,
    dv_wall_4_w1 = as.numeric(wall_4_1),
    
    dv_amtrak_1_w1 =
      case_when(
        amtrak_1_1 == "A Lot Less" ~ 7,
        amtrak_1_1 == "Somewhat Less" ~ 6,
        amtrak_1_1 == "A Little Less" ~ 5,
        amtrak_1_1 == "About What It's Doing Now" ~ 4,
        amtrak_1_1 == "A Little More" ~ 3,
        amtrak_1_1 == "Somewhat More" ~ 2,
        amtrak_1_1 == "A Lot More " ~ 1
      ),
    
    dv_amtrak_2_w1 = as.numeric(amtrak_2_1),
    dv_amtrak_3_w1 = -1 * as.numeric(amtrak_3_1) + 8,
    dv_amtrak_4_w1 = as.numeric(amtrak_4_1),
    
    dv_vets_1_w1 = -1 * as.numeric(vets_1_1) + 101,
    dv_vets_2_w1 = as.numeric(vets_2_1),
    dv_vets_3_w1 =
      case_when(
        vets_3_1 == "Strongly Oppose" ~ 1,
        vets_3_1 == "Oppose" ~ 2,
        vets_3_1 == "Somewhat Oppose" ~ 3,
        vets_3_1 == "Undecided" ~ 4,
        vets_3_1 == "Somewhat Favor" ~ 5,
        vets_3_1 == "Favor" ~ 6,
        vets_3_1 == "Strongly Favor" ~ 7
      ),
    
    dv_vets_4_w1 = as.numeric(vets_4_1),
    
    dv_flat_1_w1 =
      case_when(
        flat_1_1 == "Strongly Oppose" ~ 1,
        flat_1_1 == "Oppose" ~ 2,
        flat_1_1 == "Somewhat Oppose" ~ 3,
        flat_1_1 == "Undecided" ~ 4,
        flat_1_1 == "Somewhat Favor" ~ 5,
        flat_1_1 == "Favor" ~ 6,
        flat_1_1 == "Strongly Favor" ~ 7
      ),
    
    dv_flat_2_w1 = -1 * as.numeric(flat_2_1) + 101,
    dv_flat_3_w1 =
      case_when(
        flat_3_1 == "Strongly Oppose" ~ 1,
        flat_3_1 == "Oppose" ~ 2,
        flat_3_1 == "Somewhat Oppose" ~ 3,
        flat_3_1 == "Undecided" ~ 4,
        flat_3_1 == "Somewhat Favor" ~ 5,
        flat_3_1 == "Favor" ~ 6,
        flat_3_1 == "Strongly Favor" ~ 7
      ),
    dv_flat_4_w1 = -1 * as.numeric(flat_4_1) + 8,
    
    dv_climate_main_w1 = dv_climate_1_w1,
    dv_wall_main_w1 = dv_wall_3_w1,
    dv_amtrak_main_w1 = dv_amtrak_1_w1,
    dv_vets_main_w1 = dv_vets_2_w1,
    dv_flat_main_w1 = dv_flat_1_w1,
    
    trap_set = as.numeric(
      (
        gov_size == "Smaller government with fewer services" &
          educ_spend == "Increase spending on education"
      )
      |
        (
          gov_size == "Larger government with more services" &
            educ_spend == "Decrease spending on education"
        )
    ), 
    
    Z_correction = as.numeric(Z_correction),
    
    spend_outcomes_1 = 
      case_when(spending_outcomes_1 == "Decrease Spending" ~ -1,
                spending_outcomes_1 == "Keep it about the same" ~ 0,
                spending_outcomes_1 == "Increase spending" ~ 1),
     spend_outcomes_2 = 
      case_when(spending_outcomes_2 == "Decrease Spending" ~ -1,
                spending_outcomes_2 == "Keep it about the same" ~ 0,
                spending_outcomes_2 == "Increase spending" ~ 1),
    spend_outcomes_3 = 
      case_when(spending_outcomes_3 == "Decrease Spending" ~ -1,
                spending_outcomes_3 == "Keep it about the same" ~ 0,
                spending_outcomes_3 == "Increase spending" ~ 1),
    spend_outcomes_4 = 
      case_when(spending_outcomes_4 == "Decrease Spending" ~ -1,
                spending_outcomes_4 == "Keep it about the same" ~ 0,
                spending_outcomes_4 == "Increase spending" ~ 1),
    spend_outcomes_5 = 
      case_when(spending_outcomes_5 == "Decrease Spending" ~ -1,
                spending_outcomes_5 == "Keep it about the same" ~ 0,
                spending_outcomes_5 == "Increase spending" ~ 1),
    
    spend_outcomes_average =
      (spend_outcomes_1 +
         spend_outcomes_2 +
         spend_outcomes_3 +
         spend_outcomes_4 +
         spend_outcomes_5) / 5,
    
    Z_gays = as.numeric(Z_gays),
    gays_employers = coalesce(gays_hiring, gays_religious),
    gays_employers = 
      case_when(
        gays_employers == "Strongly agree" ~ 7 ,
        gays_employers == "Agree" ~ 6,
        gays_employers == "Somewhat agree" ~ 5,
        gays_employers == "Neither agree nor disagree" ~ 4,
        gays_employers == "Somewhat disagree" ~ 3,
        gays_employers == "Disagree" ~ 2,
        gays_employers == "Strongly disagree" ~ 1
      ),
    
    ssm_ammend = 
      case_when(
        ssm_ammend == "Strongly oppose" ~ 1 ,
        ssm_ammend == "Oppose" ~ 2,
        ssm_ammend == "Somewhat oppose" ~ 3,
        ssm_ammend == "Neither favor nor oppose" ~ 4,
        ssm_ammend == "Somewhat favor" ~ 5,
        ssm_ammend == "Favor" ~ 6,
        ssm_ammend == "Strongly favor" ~ 7
      ),
    
    ssm_exempt =
      case_when(
        ssm_exempt == "They should be excused from issuing marriage licenses that violate their religious convictions" ~ 1,
        ssm_exempt == "They should be required to issue marriage licenses to same-sex couples" ~ 0
      ), 
    prayer_ammend = 
      case_when(
        prayer_ammend == "Strongly oppose" ~ 1 ,
        prayer_ammend == "Oppose" ~ 2,
        prayer_ammend == "Somewhat oppose" ~ 3,
        prayer_ammend == "Neither favor and oppose" ~ 4,
        prayer_ammend == "Somewhat favor" ~ 5,
        prayer_ammend == "Favor" ~ 6,
        prayer_ammend == "Strongly favor" ~ 7
      ),
    
    prayer_football = 
      case_when(
        prayer_football == "Strongly agree" ~ 7 ,
        prayer_football == "Agree" ~ 6,
        prayer_football == "Somewhat agree" ~ 5,
        prayer_football == "Neither agree nor disagree" ~ 4,
        prayer_football == "Somewhat disagree" ~ 3,
        prayer_football == "Disagree" ~ 2,
        prayer_football == "Strongly disagree" ~ 1
      ),
    
    welfare = coalesce(welfare_welfare, welfare_aid),
    welfare_good = 
      case_when(welfare == "Too little" ~ 3,
                welfare == "About right" ~ 2,
                welfare == "Too much" ~ 1),
    medicaid_good = as.numeric(medicaid == "Medicaid is a good thing because our society should provide health care to poor people"),
    gov_reduce_ineq = -1 * as.numeric(substr(equiwealth, 1, 1)) + 8
  )


dat <- within(dat,{
  
  dv_wall_1_s_w1 <- dv_wall_1_w1/sd(study_1_mturk$dv_wall_1_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_wall_2_s_w1 <- dv_wall_2_w1/sd(study_1_mturk$dv_wall_2_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_wall_3_s_w1 <- dv_wall_3_w1/sd(study_1_mturk$dv_wall_3_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_wall_4_s_w1 <- dv_wall_4_w1/sd(study_1_mturk$dv_wall_4_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_amtrak_1_s_w1 <- dv_amtrak_1_w1/sd(study_1_mturk$dv_amtrak_1_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_amtrak_2_s_w1 <- dv_amtrak_2_w1/sd(study_1_mturk$dv_amtrak_2_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_amtrak_3_s_w1 <- dv_amtrak_3_w1/sd(study_1_mturk$dv_amtrak_3_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_amtrak_4_s_w1 <- dv_amtrak_4_w1/sd(study_1_mturk$dv_amtrak_4_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_vets_1_s_w1 <- dv_vets_1_w1/sd(study_1_mturk$dv_vets_1_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_vets_2_s_w1 <- dv_vets_2_w1/sd(study_1_mturk$dv_vets_2_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_vets_3_s_w1 <- dv_vets_3_w1/sd(study_1_mturk$dv_vets_3_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_vets_4_s_w1 <- dv_vets_4_w1/sd(study_1_mturk$dv_vets_4_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_flat_1_s_w1 <- dv_flat_1_w1/sd(study_1_mturk$dv_flat_1_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_flat_2_s_w1 <- dv_flat_2_w1/sd(study_1_mturk$dv_flat_2_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_flat_3_s_w1 <- dv_flat_3_w1/sd(study_1_mturk$dv_flat_3_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_flat_4_s_w1 <- dv_flat_4_w1/sd(study_1_mturk$dv_flat_4_w1[study_1_mturk$Z=="control"],na.rm = TRUE)
  dv_wall_main_s_w1 <- dv_wall_3_s_w1
  dv_amtrak_main_s_w1 <- dv_amtrak_1_s_w1
  dv_vets_main_s_w1 <- dv_vets_2_s_w1
  dv_flat_main_s_w1 <- dv_flat_1_s_w1
  
})

dat <- within(dat,{
  dv_wall_scale_w1 <- predict(fa_wall, data = dat %>% select(dv_wall_1_s_w1, dv_wall_2_s_w1, dv_wall_3_s_w1, dv_wall_4_s_w1))[,1]
  dv_amtrak_scale_w1 <- predict(fa_amtrak, data = dat %>% select(dv_amtrak_1_s_w1, dv_amtrak_2_s_w1, dv_amtrak_3_s_w1, dv_amtrak_4_s_w1))[,1]
  dv_vets_scale_w1 <- predict(fa_vets, data = dat %>% select(dv_vets_1_s_w1, dv_vets_2_s_w1, dv_vets_3_s_w1, dv_vets_4_s_w1))[,1]
  dv_flat_scale_w1 <- predict(fa_flat, data = dat %>% select(dv_flat_1_s_w1, dv_flat_2_s_w1, dv_flat_3_s_w1, dv_flat_4_s_w1))[,1]
})


dat <-
  dat %>%
  mutate(
    Z_americans_first = as.numeric(Z_americans_first),
    Z_russia_bad = as.numeric(Z_a == 1),
    Y_americans_there = as.numeric(Y_a == "Yes"),
    Y_russians_here = as.numeric(Y_b == "Yes")
  )


dat <-
  dat %>%
  select(
    starts_with("lucid_"),
    pid_7,
    pid_3_cat,
    ideo_3_cat,
    ideo_5,
    educ_5_cat,
    educ_5,
    race_4,
    female,
    age_5,
    Z,
    starts_with("dv_"),
    gov_size,
    trap_set,
    Z_correction,
    starts_with("spend_"),
    Z_gays,
    gays_employers,
    starts_with("ssm_"),
    starts_with("prayer_"),
    Z_welfare,
    welfare,
    welfare_good,
    medicaid_good,
    gov_reduce_ineq,
    Z_americans_first,
    Z_russia_bad,
    Y_americans_there,
    Y_russians_here,
  )

write_rds(dat, "data/clean/studies_1_2_replications_lucid_cleaned.rds")

